﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;

namespace BAL
{
    public partial class UNIF_Form : Form
    {

        private MeasuerService.BarcodeJudgeServices bjs = new BAL.MeasuerService.BarcodeJudgeServices();
        private Socket isco = null;
        private string ipAddr = "192.168.30.203";
        int port = 5566;


        public UNIF_Form()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MeasuerMenu menuform = new MeasuerMenu();
            menuform.Show();
            this.Hide();
        }

        private void label2_ParentChanged(object sender, EventArgs e)
        {

        }

        private void UNIF_Form_Load(object sender, EventArgs e)
        {
            
            
       
            isco = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            IPAddress address = IPAddress.Parse(ipAddr);
            IPEndPoint point = new IPEndPoint(address, port);
            //IAsyncResult iay = isco.BeginConnect(point, null, null);
            //iay.AsyncWaitHandle.WaitOne(1000, true);
            try
            {
                isco.Connect(point);
               
                label8.Text = "OK";
                label8.BackColor = Color.Green;
            }
            catch
            {
                label8.Text = "NG";
                label8.BackColor = Color.Red;
            }
            textBox1.Focus();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string measuer = "";
            string meavalue = "";
            
            //label6.Text = "制品编号" + bjs.GetKeycode(textBox1.Text.Trim());

            isco = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress address = IPAddress.Parse(ipAddr);
            IPEndPoint point = new IPEndPoint(address, port);
           
                
                if (textBox1.Text.Trim().Length == 7)
                {
                    try
                    {
                        isco.Connect(point);
                        label8.Text = "OK";
                        label8.BackColor = Color.Green;
                        label4.Text = "判定:" + bjs.UnifJudge(textBox1.Text.Trim(), DateTime.Now.ToString(), out measuer, out meavalue);
                        label3.Text = "条码:" + textBox1.Text.Trim();
                        label5.Text = "扫描时间:" + DateTime.Now;

                        label6.Text = "RFV-REP基准:" + measuer + "N";

                        label2.Text = "测量值:" + meavalue + "N";
                    }
                    catch
                    {
                        label8.Text = "NG";
                        label8.BackColor = Color.Red;
                        MessageBox.Show("请检查网络!!", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);                  
                    }

                    
                    textBox1.Text = "";
                }

            
        }
    }
}